package com.whlilong.jxc.view;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Table;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IViewPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.WorkbenchException;
import org.eclipse.ui.part.ViewPart;

import com.whlilong.jxc.constants.IAppConstants;
import com.whlilong.jxc.input.AccountInfoInput;
import com.whlilong.jxc.input.DepartInfoInput;
import com.whlilong.jxc.input.ClientInfoEditorInput;
import com.whlilong.jxc.input.BusinessInfoEditorInput;
import com.whlilong.jxc.input.AidInfoInput;
import com.whlilong.jxc.input.GoodsInfoInput;
import com.whlilong.jxc.input.DistrictEditorInput;
import com.whlilong.jxc.input.MerchantTypeEditorInput;
import com.whlilong.jxc.input.PaymentInfoInput;
import com.whlilong.jxc.input.BrandInfoInput;
import com.whlilong.jxc.input.StaffInfoInput;
import com.whlilong.jxc.model.Sysfun;
import com.whlilong.jxc.sys.CacheImage;
import com.whlilong.jxc.sys.IImageKey;
import com.whlilong.jxc.util.ViewerChange;

import org.eclipse.swt.widgets.Label;

/**
 * 档案管理视图
 * 
 * @author sjy
 * 
 */
public class FileManagerView extends ViewPart implements ISelectionListener {
	private final IWorkbenchWindow window = PlatformUI.getWorkbench()
			.getActiveWorkbenchWindow();

	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Button button5;
	private Button button6;
	private Button button7;
	private Button button8;
	private Button button9;
	private Button button10;
	private Button button12;
	private Button button17;
	private Button button18;
	private Button kstype;

	public FileManagerView() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void createPartControl(Composite parent) {
		// TODO Auto-generated method stub
		/*
		 * IViewPart vp = getViewSite().getPage().findView(
		 * IAppConstants.sysfunction_view_id); Table table =
		 * ((SystemManagerViewer) vp).getTableViewer().getTable();
		 * 
		 * Sysfun sysfun = (Sysfun) table.getItem(table.getSelectionIndex())
		 * .getData();
		 * 
		 * @SuppressWarnings("unused") int sysno = sysfun.getId();
		 */
		createButtons(parent);
		PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage()
				.addSelectionListener(this);
	}

	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	private void createButtons(Composite parent) {
		// TODO Auto-generated method stub
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.makeColumnsEqualWidth = true;
		layout.numColumns = 4;
		layout.marginTop = 10;
		layout.marginBottom = 10;
		layout.marginRight = 10;
		layout.marginLeft = 10;

		composite.setLayout(layout);
		composite.setBackgroundImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.FUNCTIONS_BG));
		GridData gd = new GridData(GridData.CENTER);
		gd.verticalIndent = 20;
		gd.horizontalIndent = 30;
		gd.widthHint = 130;
		gd.heightHint = 50;

		button1 = new Button(composite, SWT.NONE);
		button1.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.KESHANGINFO));
		button1.setText("客商地区");
		button1.setToolTipText(button1.getText());
		button1.setLayoutData(gd);
		button1.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				// String info = button1.getText();
				// createKeshangInfoDialog(info);

				DistrictEditorInput input = new DistrictEditorInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.distractEditor_id, true);
					
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}
		});

		kstype = new Button(composite, SWT.PUSH);
		kstype.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.kstype));
		kstype.setText("客商类别");
		kstype.setToolTipText(kstype.getText());
		kstype.setLayoutData(gd);
		kstype.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				MerchantTypeEditorInput input = new MerchantTypeEditorInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.merchantTypeEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}
		});
		button12 = new Button(composite, SWT.PUSH);
		button12.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.goodsbrand));
		button12.setText("商品品牌");
		button12.setToolTipText(button12.getText());
		button12.setLayoutData(gd);
		button12.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				BrandInfoInput input = new BrandInfoInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.brandInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button3 = new Button(composite, SWT.PUSH);
		button3.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.kehuoinfo));
		button3.setText("客户信息");
		button3.setToolTipText(button3.getText());
		button3.setLayoutData(gd);
		button3.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				ClientInfoEditorInput input = new ClientInfoEditorInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.clientInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button2 = new Button(composite, SWT.PUSH);
		button2.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.huoshanginfo));
		button2.setText("货商信息");
		button2.setToolTipText(button2.getText());
		button2.setLayoutData(gd);
		button2.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				BusinessInfoEditorInput input = new BusinessInfoEditorInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.businessInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button4 = new Button(composite, SWT.PUSH);
		button4.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.goodsinfo));
		button4.setText("商品信息");
		button4.setToolTipText(button4.getText());
		button4.setLayoutData(gd);
		button4.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				GoodsInfoInput input = new GoodsInfoInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.goodsInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button5 = new Button(composite, SWT.PUSH);
		button5.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.bumeninfo));
		button5.setText("部门信息");
		button5.setToolTipText(button5.getText());
		button5.setLayoutData(gd);
		button5.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				DepartInfoInput input = new DepartInfoInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.departInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button6 = new Button(composite, SWT.PUSH);
		button6.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.yuangonginfo));
		button6.setText("员工信息");
		button6.setToolTipText(button6.getText());
		button6.setLayoutData(gd);
		button6.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				StaffInfoInput input = new StaffInfoInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.staffInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button7 = new Button(composite, SWT.PUSH);
		button7.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.shouzhiinfo));
		button7.setText("收支信息");
		button7.setToolTipText(button7.getText());
		button7.setLayoutData(gd);
		button7.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				PaymentInfoInput input = new PaymentInfoInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.paymentInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button8 = new Button(composite, SWT.PUSH);
		button8.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.zhanghuinfo));
		button8.setText("账户信息");
		button8.setToolTipText(button8.getText());
		button8.setLayoutData(gd);
		button8.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				AccountInfoInput input = new AccountInfoInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.accountInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button9 = new Button(composite, SWT.PUSH);
		button9.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.fuzhuinfo));
		button9.setText("辅助信息");
		button9.setToolTipText(button9.getText());
		button9.setLayoutData(gd);
		button9.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				AidInfoInput input = new AidInfoInput();
				try {
					window.getActivePage().savePerspective();
					window.getActivePage().close();
					window.openPage(IAppConstants.editor_perspective, null);
					window.getShell().setMaximized(true);
					window.getActivePage().openEditor(input,
							IAppConstants.aidInfoEditor_id, true);
				} catch (PartInitException e1) {
					e1.printStackTrace();
				} catch (WorkbenchException e2) {
					e2.printStackTrace();
				}
			}

		});

		button10 = new Button(composite, SWT.PUSH);
		button10.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.infochuru));
		button10.setText("信息导入导出");
		button10.setToolTipText(button10.getText());
		button10.setLayoutData(gd);

		button17 = new Button(composite, SWT.PUSH);
		button17.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.logoff));
		button17.setText("注销用户");
		button17.setToolTipText(button17.getText());
		button17.setLayoutData(gd);
		button17.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				PlatformUI.getWorkbench().restart();
			}

		});

		button18 = new Button(composite, SWT.PUSH);
		button18.setImage(CacheImage.getINSTANCE().getImage(
				IAppConstants.APPLICATION_ID, IImageKey.exit));
		button18.setText("退出系统");
		button18.setToolTipText(button18.getText());
		button18.setLayoutData(gd);
		new Label(composite, SWT.NONE);
		new Label(composite, SWT.NONE);
		button18.addMouseListener(new MouseListener() {

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				// TODO Auto-generated method stub

			}

			@Override
			public void mouseDown(MouseEvent e) {
				// TODO Auto-generated method stub
				PlatformUI.getWorkbench().close();
			}

			@Override
			public void mouseUp(MouseEvent e) {
				// TODO Auto-generated method stub

			}

		});

	}

	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		// TODO Auto-generated method stub
		if (!selection.isEmpty()) {
			IViewPart vp = getViewSite().getPage().findView(
					IAppConstants.systemManagerview_id);

			Table table = ((SystemManagerView) vp).getTableViewer().getTable();
			ViewerChange.getInstance().refreshView(table);
		}
	}
}
